/*
# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.
# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.
# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

*/

using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;

public class GiftMenu : MonoBehaviour {

	[Tooltip("No Internet gameobject.")]
	public GameObject noInternet;
	[Tooltip("Gift button gameobject.")]
	public GameObject giftButton;
	[Tooltip("Fade in/out animation.")]
	public Animation fade;

	//If player pressed get now gift with ad.
	public void GetNow()
	{
		//Start fade in animation.
		fade.Play("FadeIn");
		//Show ads.
		StartCoroutine(ShowAd());
	}

	//Fade out animation.
	public void FadeOut()
	{
		fade.Play("FadeOut");
	}	

	IEnumerator ShowAd()
	{
		//Wait when fade animation ends.
		yield return new WaitForSeconds(0.5f);

        string videoAdid = VideData.Instance.GetVide();
        TTSDK.TTRewardedVideoAd r = AdMgr.Instance.ShowVideoAdNew(videoAdid);

        //创建激励视频关闭回调
        r.OnClose += (bol, val) =>
        {
            Debug.Log("关闭广告 ，是否已经看完" + bol);
            //看完广告获得提示
            if (bol)
            {
                //Add coins to player wallet.
                Wallet.AddCoins(10);
                Wallet.CoinBlast();
                //Reset gift timer.
                Timer.giftTimer = 1800;
                //Update achievements stats.
                PlayerPrefs.SetInt("CollectGifts", (PlayerPrefs.GetInt("CollectGifts") + 1));
                FadeOut();
                MaidianNew.MaidianBackCallback(" ");
                Debug.Log("广告看完");
            }
            else
            {
                Debug.Log("广告未看完");
            }
        };

        //创建报错回调  
        r.OnError += (code, errorMessage) =>
        {

            Debug.Log("code:" + code + "错误信息：" + errorMessage);
        };

        //展示广告（新版本必须调用，不然无法展示出来广告）
        r.Show();
    //If device has Internet connection.
	#if UNITY_ADS
			if(Coroutines.isConnected)
			{
				//Load ad.
				if (Advertisement.IsReady("rewardedVideo"))
				{
					StartCoroutine(StartVideoAd());				
				}	
			}	
			//If device has no Internet connection.
			else
			{
				//Enable No Internet gameobject and disable gift menu.
				noInternet.SetActive(true);
				gameObject.SetActive(false);
			}
	#endif
	}

	IEnumerator StartVideoAd()
	{
		yield return new WaitForSeconds(0.5f);		
		//Show ad.
#if UNITY_ADS
		Advertisement.Show("rewardedVideo");
		//Add coins to player wallet.
		Wallet.AddCoins(100);
		Wallet.CoinBlast();
		//Reset gift timer.
		Timer.giftTimer = 1800;
		//Update achievements stats.
		PlayerPrefs.SetInt("CollectGifts", (PlayerPrefs.GetInt("CollectGifts")+1));
		FadeOut();			
#endif
	}

	//If pressed claim button with no ads.
	public void Claim()
	{
		//Add coins to player wallet.
		Wallet.AddCoins(10);
		Wallet.CoinBlast();	
		//Reset timer.
		Timer.giftTimer = 1800;
		//Reset gift image and animation.
		giftButton.GetComponent<Image>().color = Color.white;
		transform.GetChild(4).GetChild(0).GetComponent<Image>().color = Color.white;
		//Update achievements stats.
		PlayerPrefs.SetInt("CollectGifts", (PlayerPrefs.GetInt("CollectGifts")+1));
	}
}
